/**
 * Created by Administrator on 2018/10/18.
 */

// 格式化状态
function statusFormatter(value) {
    var spanHtml = '';
    switch (value) {
        case '已提交':
            spanHtml = '<span class="label label-primary">已提交</span>';
            break;
        case '已完成':
            spanHtml = '<span class="label label-success">已完成</span>';
            break;
        case '已驳回':
            spanHtml = '<span class="label label-danger">已驳回</span>';
            break;
        case '进行中':
            spanHtml = '<span class="label label-info">进行中</span>';
            break;
        case '未开始':
            spanHtml = '<span class="label label-warning">未开始</span>';
            break;
        case '已超期':
            spanHtml = '<span class="label label-danger">已超期</span>';
            break;
    }
    // console.log(spanHtml);
    return spanHtml;
}
$(function () {

    var user = getUser();
    $("#jqGrid").jqGrid({
        url: baseURL + 'generator/projectentity/queryProject/' + user.userId,
        datatype: "json",
        colModel: [
            {label: '项目编号', name: 'projectNumber', index: 'project_number', width: 80, sortable: true},
            {label: '项目名称', name: 'projectName', index: 'project_name', width: 80, sortable: false},
            {label: '项目状态', name: 'projectStatus', index: 'project_status', width: 80, sortable: false},
            {label: '当前任务', name: 'projectTask', index: 'projectTask', width: 80, sortable: false},
            {
                label: '任务状态', name: 'projectTaskStatus', index: 'projectTaskStatus', width: 80, sortable: false,
                formatter: function (value) {
                    return statusFormatter(value);
                }
            },
            {
                label: '电压类型',
                name: 'voltageType',
                index: 'voltage_type',
                width: 80,
                sortable: false,
                formatter: function (value, options, row) {
                    console.log(value);
                    if (value == "1") {
                        return '<span >' + '输电' + '</span>';
                    } else {
                        return '<span >' + '配电' + '</span>';
                    }
                }
            },
            {label: '项目经理', name: 'projectManager', index: 'project_manager', width: 80, sortable: false},
            {label: '申报时间', name: 'projectTime', index: 'project_time', width: 80, sortable: true},
            {label: '开始时间', name: 'startTime', index: 'start_time', width: 80, sortable: false},
            {label: '结束时间', name: 'endTime', index: 'end_time', width: 80, sortable: false},
            {
                label: '操作', name: 'endTime', width: 80, sortable: false, formatter: function (value, options, row) {
                    console.log(row);
                    return '<a class="btn label-primary btn-xs" id="btn-milepost" href="javascript:void(0);" data-id="' + row.id + '">' + '详情' + '</a>'
                        + '&nbsp;&nbsp;&nbsp;<a class="btn label-primary btn-xs" id="btn-submit" href="javascript:void(0);" data-id="' + row.id + '">终止项目</a>';
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: false,
        multiSort: false,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});

            $('#jqGrid').on('click', '#btn-submit', function (e) {
                var projectId = $(this).attr('data-id');
                confirm('确定要终止该项目吗？', function () {
                    var projectStatus = "3";
                    var data = {
                        id: projectId,
                        projectStatus: projectStatus
                    };
                    $.ajax({
                        type: "POST",
                        url: baseURL + "generator/projectentity/update",
                        contentType: "application/json",
                        data: JSON.stringify(data),
                        success: function (r) {
                            if (r.code === 0) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                });
            });

            $('#jqGrid').on('click', '#btn-milepost', function (e) {
                var projectId = $(this).attr('data-id');
                var pWindow = window.parent;
                var oIframe = $('#main-content iframe', pWindow.document);

                // 改变首页的 indexVm 的 data，触发 updated
                pWindow.indexVm.main = 'modules/project/mileposttask.html?projectId=' + projectId;
                pWindow.location.hash = 'modules/project/mileposttask.html?projectId=' + projectId;
                oIframe.attr('src', 'modules/project/mileposttask.html?projectId=' + projectId
                );
            });
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        projectEntity: {},
        searchOptions: {
            projectStatus: '',
            projectName: '',
            voltageType: ''
        }
    },
    mounted: function () {
        var _this = this;
        _this.searchOptions.voltageType = "0";
    },
    created: function () {
        // console.log(1);
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.projectEntity = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.projectEntity.id == null ? "generator/projectentity/save" : "generator/projectentity/update";
            vm.projectEntity.projectStatus = 0; // 创建时默认项目进行中
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.projectEntity),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "generator/projectentity/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "generator/projectentity/info/" + id, function (r) {
                vm.projectEntity = r.projectEntity;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            // console.log(page);
            console.log(vm.searchOptions);
            // console.log(vm.projectEntity);
            var searchOptions = vm.searchOptions;
            $("#jqGrid").jqGrid("setGridParam", {postData: searchOptions});
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});
